package com.yxlgateway.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxlcommon.common.dto.LikesDTO;
import com.yxlcommon.common.mapper.LikesMapper;
import com.yxlcommon.common.mapper.WorkCommentMapper;
import com.yxlcommon.common.mapper.WorksInfoMapper;
import com.yxlcommon.common.pojo.Likes;
import com.yxlcommon.common.pojo.RespBean;
import com.yxlgateway.service.LikesService;
import com.yxlcommon.common.vo.LikesVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.Cursor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author: yxl
 * @since: 2022-04-17
 **/
@Service
@Slf4j
public class LikesServiceImpl extends ServiceImpl<LikesMapper, Likes> implements LikesService {

    @Autowired
    private RedisServiceImpl redisService;
    @Autowired
    private LikesMapper likesMapper;
    @Autowired
    private WorksInfoMapper worksInfoMapper;
    @Autowired
    private WorkCommentMapper workCommentMapper;

    @Transactional
    @Override
    public void transLikedFromRedisToDB() {
        List<Likes> likes = redisService.getLikeDataFromRedis();

        for (Likes like : likes) {
            Date date = new Date();
            Timestamp timestamp = new Timestamp(date.getTime());
            Likes likes1 = likesMapper.selectByObjIdAndUserId(like.getObjId(), like.getUserId(), like.getType());
            if (likes1 == null) {
                like.setCreateTime(timestamp);
                likesMapper.insert(like);
            } else {
                likesMapper.updateById(like);
            }
        }
    }

    @Override
    public void transLikedCountFromRedisToDB() {
        Cursor<Map.Entry<Object, Object>> cursor = redisService.getLikedCountFromRedis();
        while (cursor.hasNext()) {
            Map.Entry<Object, Object> map = cursor.next();
            String key = (String) map.getKey();

            String[] split = key.split("::");
            byte type = Byte.parseByte(split[1]);
            long objId = Long.parseLong(split[2]);
            long likesNum = (Long) map.getValue();
            if (type == 1) {
                worksInfoMapper.updateWorkLikesNum(objId, likesNum);
            } else {
                workCommentMapper.updateContentPraise(objId, likesNum);
            }
        }
    }

    @Override
    public List<Map<String, Object>> selectLikeInfoByType(LikesDTO likesDTO) {
        return likesMapper.selectLikesInfoByType(likesDTO.getType());
    }

    @Override
    public RespBean<Map<String, Object>> selectLike(Likes likes) {

        Map<String, Object> dataMap = new ConcurrentHashMap<>();
        dataMap.put("countLikes", likesMapper.selectCount(new QueryWrapper<Likes>().eq("obj_id", likes.getObjId()).eq("type", 1)));

        dataMap.put("isLike", likesMapper.selectCount(new QueryWrapper<Likes>().eq("user_id",likes.getUserId()).eq("obj_id",likes.getObjId()).eq("type",likes.getType())));
        return RespBean.operationSuccess("查询成功", 200, dataMap);
    }

    @Override
    public RespBean<Integer> unSave(Likes likes) {
        Long likeId = likesMapper.selectId(likes.getObjId(), likes.getUserId(), likes.getType());
        if (likesMapper.deleteById(likeId) == 1) {
            return RespBean.operationSuccess("删除成功", 200, 1);
        }
        return RespBean.operationFail("删除失败");
    }

    @Override
    public RespBean<List<LikesVo>> selectWorksByLike(Likes likes) {
        return RespBean.operationSuccess("查询成功",200,likesMapper.selectWorksByLike(likes.getUserId()));
    }
}
